package com.jitu.bigscreen.job;

import com.alibaba.fastjson.JSONObject;
import com.jitu.bigscreen.entity.ParkingArea;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

@Configuration
@EnableScheduling
public class GetParkingNumber {

    private static final int TIME_OUT = 3000;

    @Value("${jitu.request-url}")
    private String url;

    @Scheduled(cron = "${jitu.send-time}")
    public void task() {

        JSONObject data = JSONObject.parseObject(request(url).get("data").toString());

        // 总共多少条数据
        Integer total = Integer.parseInt(data.get("total").toString());

        // 总共页数
        Integer pages = Integer.parseInt(data.get("pages").toString());

        // 每页多少条数据
        Integer pageSize = Integer.parseInt(data.get("pageSize").toString());

        // 当前页码
        Integer pageNum = Integer.parseInt(data.get("pageNum").toString());

        // 主要数据
        List<ParkingArea> areas = JSONObject.parseArray(data.get("list").toString(), ParkingArea.class);

        send(areas);

        System.out.println(LocalDateTime.now());
    }

    public JSONObject request(String url) {

        String m = null;

        CloseableHttpClient client = null;
        try {
            client = HttpClients.createDefault();
            // 注意有的网页不支持 post 请求
            HttpPost httpPost = new HttpPost(url);

            // 配置请求的超时设置
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(TIME_OUT)
                    .setConnectTimeout(TIME_OUT)
                    .setSocketTimeout(TIME_OUT).build();
            httpPost.setConfig(requestConfig);

            httpPost.setHeader("charset", "UTF-8");
            httpPost.setHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);

            String param = "start=1&limit=1000";
            StringEntity paramEntity= new StringEntity(param, StandardCharsets.UTF_8);
            paramEntity.setContentEncoding(StandardCharsets.UTF_8.toString());
            httpPost.setEntity(paramEntity);

            // 响应内容
            CloseableHttpResponse response = client.execute(httpPost);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode == 200) {

                // 成功响应，读取返回内容
                HttpEntity entity = response.getEntity();
                // 转换entity为字符串
                m = EntityUtils.toString(entity, StandardCharsets.UTF_8);

                System.out.println(m);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSONObject.parseObject(m);
    }

    public void send(List<ParkingArea> areas) {
        // TODO 发送消息到大屏

    }

}
